// Decompiled by DJ v2.9.9.61 Copyright 2000 Atanas Neshkov  Date: 2008-01-22 ���� 7:24:35
// Home Page : http://members.fortunecity.com/neshkov/dj.html  - Check often for new version!
// Decompiler options: packimports(3) 
// Source File Name:   ServiceMsgFormat.java

package com.pushmsg;

import java.io.*;

// Referenced classes of package pushmsg:
//            Constants, EMSObject

public class ServiceMsgFormat
    implements Constants
{

    public ServiceMsgFormat(int i)
    {
        numContent = i;
    }

    public void setEmsObject(EMSObject aemsobject[])
    {
        pEmsObj = aemsobject;
    }

    public void setChargeType(int i)
    {
        chargeType = i;
    }

    public void setUserId(String s)
    {
        userid = s;
    }

    public void setMsgType(int i)
    {
        MsgType = i;
    }

    public void setDataType(int i)
    {
        dataType = i;
    }

    public void setDa(String s)
        throws IOException
    {
        if(s.getBytes().length > 21)
        {
            throw new IOException("DA size is too big");
        } else
        {
            da = s;
            return;
        }
    }

    public void setOa(String s)
        throws IOException
    {
        if(s.getBytes().length > 21)
        {
            throw new IOException("OA size is too big");
        } else
        {
            oa = s;
            return;
        }
    }

    public void setMsgFmt(int i)
    {
        msgFmt = i;
    }

    public void setTrId(String s)
    {
        trid = s;
    }

    public void setPointCode(int i)
    {
        point = i;
    }

    public void setUserData(String as[], String s)
        throws IOException
    {
        int i = 0;
        int j = 0;
        short word0 = (short)s.length();
        byte abyte0[] = new byte[1024];
        byte abyte1[] = s.getBytes();
        i += 2;
        abyte0[i++] = 1;
        abyte0[i++] = (byte)((word0 & 0xff00) >> 8);
        abyte0[i++] = (byte)(word0 & 0xff);
        for(short word1 = 0; word1 < word0; word1++)
            abyte0[i++] = abyte1[word1];

        for(int k = 0; k < numContent; k++)
            if(pEmsObj[k].ElementType == 10)
            {
                abyte0[i++] = pEmsObj[k].ElementType;
                abyte0[i++] = 3;
                abyte0[i++] = pEmsObj[k].pos;
                abyte0[i++] = pEmsObj[k].len;
                abyte0[i++] = pEmsObj[k].style;
            } else
            if(pEmsObj[k].ElementType == 11 || pEmsObj[k].ElementType == 13)
            {
                abyte0[i++] = pEmsObj[k].ElementType;
                abyte0[i++] = 2;
                abyte0[i++] = pEmsObj[k].pos;
                abyte0[i++] = Byte.parseByte(pEmsObj[k].content_id);
            } else
            {
                try
                {
                    File file = new File("/home01/anycall/mobile/admincp/contents/" + as[k]);
                    FileInputStream fileinputstream = new FileInputStream(file);
                    int i1;
                    while((i1 = fileinputstream.read()) != -1) 
                    {
                        abyte0[i + j] = (byte)i1;
                        j++;
                        if(i + j >= 1021)
                            throw new IOException("> User data size is too big.");
                    }
                    fileinputstream.close();
                    if(pEmsObj[k].ElementType == 12 || pEmsObj[k].ElementType == 18)
                        abyte0[i + 3] = (byte)(pEmsObj[k].pos & 0xff);
                    else
                        abyte0[i + 2] = (byte)(pEmsObj[k].pos & 0xff);
                    i += j;
                    j = 0;
                }
                catch(FileNotFoundException filenotfoundexception)
                {
                    throw new IOException("Could not found file : " + as);
                }
                catch(IOException ioexception)
                {
                    throw ioexception;
                }
                catch(NullPointerException nullpointerexception)
                {
                    throw new IOException("Null Pointer error from setUserData");
                }
            }

        i -= 2;
        abyte0[0] = (byte)((i & 0xff00) >> 8);
        abyte0[1] = (byte)(i & 0xff);
        userData = new byte[i + 2];
        for(int l = 0; l < userData.length; l++)
            userData[l] = abyte0[l];

    }

    public void setUserData(String as[], byte abyte0[])
        throws IOException
    {
        int i = 0;
        int j = 0;
        short word0 = (short)abyte0.length;
        byte abyte1[] = new byte[1024];
        byte abyte2[] = abyte0;
        i += 2;
        abyte1[i++] = 1;
        abyte1[i++] = (byte)((word0 & 0xff00) >> 8);
        abyte1[i++] = (byte)(word0 & 0xff);
        for(short word1 = 0; word1 < word0; word1++)
            abyte1[i++] = abyte2[word1];

        for(int k = 0; k < numContent; k++)
            if(pEmsObj[k].ElementType == 10)
            {
                abyte1[i++] = pEmsObj[k].ElementType;
                abyte1[i++] = 3;
                abyte1[i++] = pEmsObj[k].pos;
                abyte1[i++] = pEmsObj[k].len;
                abyte1[i++] = pEmsObj[k].style;
            } else
            if(pEmsObj[k].ElementType == 11 || pEmsObj[k].ElementType == 13)
            {
                abyte1[i++] = pEmsObj[k].ElementType;
                abyte1[i++] = 2;
                abyte1[i++] = pEmsObj[k].pos;
                abyte1[i++] = Byte.parseByte(pEmsObj[k].content_id);
            } else
            {
                try
                {
                    File file = new File("/home01/anycall/mobile/admincp/contents/" + as[k]);
                    FileInputStream fileinputstream = new FileInputStream(file);
                    int i1;
                    while((i1 = fileinputstream.read()) != -1) 
                    {
                        abyte1[i + j] = (byte)i1;
                        j++;
                        if(i + j >= 1021)
                            throw new IOException("> User data size is too big.");
                    }
                    fileinputstream.close();
                    if(pEmsObj[k].ElementType == 12 || pEmsObj[k].ElementType == 18)
                        abyte1[i + 3] = (byte)(pEmsObj[k].pos & 0xff);
                    else
                        abyte1[i + 2] = (byte)(pEmsObj[k].pos & 0xff);
                    i += j;
                    j = 0;
                }
                catch(FileNotFoundException filenotfoundexception)
                {
                    throw new IOException("Could not found file : " + as);
                }
                catch(IOException ioexception)
                {
                    throw ioexception;
                }
                catch(NullPointerException nullpointerexception)
                {
                    throw new IOException("Null Pointer error from setUserData");
                }
            }

        i -= 2;
        abyte1[0] = (byte)((i & 0xff00) >> 8);
        abyte1[1] = (byte)(i & 0xff);
        userData = new byte[i + 2];
        for(int l = 0; l < userData.length; l++)
            userData[l] = abyte1[l];

    }

    public void encode()
    {
        byte byte0 = 12;
        int i = 0;
        for(int j = 0; j < numContent; j++)
            if(pEmsObj[j].ElementType != 10)
                i++;

        int k = byte0 + 21 + 21 + 1 + 1 + userData.length + 20 + 16 + 4;
        packet = new byte[k];
        int l = 0;
        packet[l++] = (byte)(k >> 24 & 0xff);
        packet[l++] = (byte)(k >> 16 & 0xff);
        packet[l++] = (byte)(k >> 8 & 0xff);
        packet[l++] = (byte)(k & 0xff);
        packet[l++] = (byte)(dataType & 0xff);
        packet[l++] = (byte)(chargeType & 0xff);
        packet[l++] = (byte)(MsgType & 0xff);
        for(int i1 = 0; i1 < userData.length; i1++)
            packet[l++] = userData[i1];

        byte abyte0[] = da.getBytes();
        for(int j1 = 0; j1 < abyte0.length; j1++)
            packet[l + j1] = abyte0[j1];

        l += 21;
        abyte0 = oa.getBytes();
        for(int k1 = 0; k1 < abyte0.length; k1++)
            packet[l + k1] = abyte0[k1];

        l += 21;
        packet[l++] = (byte)(msgFmt & 0xff);
        abyte0 = userid.getBytes();
        for(int l1 = 0; l1 < 20; l1++)
            if(l1 >= abyte0.length)
                packet[l + l1] = 0;
            else
                packet[l + l1] = abyte0[l1];

        l += 20;
        abyte0 = trid.getBytes();
        for(int i2 = 0; i2 < 16; i2++)
            packet[l + i2] = abyte0[i2];

        l += 16;
        packet[l++] = (byte)(point >> 24 & 0xff);
        packet[l++] = (byte)(point >> 16 & 0xff);
        packet[l++] = (byte)(point >> 8 & 0xff);
        packet[l] = (byte)(point & 0xff);
    }

    public byte[] getPacket()
        throws IOException
    {
        if(packet != null)
            return packet;
        else
            throw new IOException("packet is not maked");
    }

    int chargeType;
    int dataType;
    int numContent;
    byte userData[];
    int MsgType;
    String da;
    String oa;
    String userid;
    String trid;
    int msgFmt;
    int point;
    byte packet[];
    EMSObject pEmsObj[];
}